package com.yulongtian.oneProblemEveryDay.month202303;

/**
 * https://leetcode.cn/problems/minimum-recolors-to-get-k-consecutive-black-blocks/
 *
 * @author yulongTian
 * @create 2023-03-09 12:13
 */
public class Test07 {
    public static void main(String[] args) {
        System.out.println(minimumRecolors("WBBWWBBWBW",10));
    }

    //WBBWWBBWBW k=7
    public static int minimumRecolors(String blocks, int k) {
        int index = 0;
        //0->W  1->B
        int[] cnt = new int[2];
        while (index < k) {
            char c = blocks.charAt(index);
            if (c == 'W') {
                cnt[0]++;
            }
            index++;
        }
        if (cnt[1] >= k) {
            return 0;
        }
        int ans = cnt[0];
        //不断后移
        for (; index < blocks.length(); index++) {
            //去除前面的
            char c1 = blocks.charAt(index - k);
            if (c1 == 'W') {
                cnt[0]--;
            }
            //加上后面的
            char c2 = blocks.charAt(index);
            if (c2 == 'W') {
                cnt[0]++;
            }
            ans = Math.min(ans, cnt[0]);
        }
        return ans;
    }


}
